#include "main.h"

#define LED0  3


/* FatFs文件系统对象 */
FATFS fs; 
/* 文件对象 */
FIL fnew;    
/* 文件操作结果 */
FRESULT res_flash; 
/* 文件成功读写数量 */
UINT fnum;   
/* 读缓冲区 */
BYTE ReadBuffer[2048]={0};  
/* 写缓冲区*/
BYTE WriteBuffer[] = "do not go gentle into that good night";

    char user_name[NAME_BUF_LEN] = {0};
#if !USER_REGISTERED
    char write_buf[NAME_BUF_LEN] = USER_NAME;
#endif /*USER_REGISTERED*/  

/*主函数*/
int main(void)
{  
    /*初始化LED*/
    pin_set(LED0,GPIO_Mode_Out_PP);
    
    /*初始化串口*/
    usart_init(115200);

#if 1    
    /*初始化EEPROM*/
    ee_CheckOk();
    
#if !USER_REGISTERED
    ee_WriteBytes((uint8_t *)write_buf,0,NAME_BUF_LEN);
    delay_ms(2000);
#endif /*USER_REGISTERED*/    
    ee_ReadBytes((uint8_t *)user_name,0,NAME_BUF_LEN);
    printf(">>>user name is %s\r\n\r\n",user_name);
    
/*-------------------初始化FLASH及挂载文件系统-----------------------*/    
	/*在外部SPI Flash挂载文件系统，文件系统挂载时会对SPI设备初始化
	初始化函数调用流程如下
	f_mount()->find_volume()->disk_initialize->SPI_FLASH_Init()*/
	res_flash = f_mount(&fs,"1:",1);  

    
/*----------------------- 格式化测试 -----------------*/  
    /* 如果没有文件系统就格式化创建创建文件系统 */
    if(res_flash == FR_NO_FILESYSTEM)
    {
        printf(">>>FLASH have not filesystem,is going to formate...\r\n");
        /* 格式化 */
		res_flash=f_mkfs("1:",0,0);							
		
		if(res_flash == FR_OK)
		{
            printf(">>>FLASH has been formated!\r\n");
            /* 格式化后，先取消挂载 */
			res_flash = f_mount(NULL,"1:",1);			
            /* 重新挂载	*/			
			res_flash = f_mount(&fs,"1:",1);
        }
		else
		{
			printf(">>>formate failed!\r\n");
			while(1)
            {
                delay_ms(200);
                pin_put(LED0,PIN_HIGH);
                delay_ms(200);
                pin_put(LED0,PIN_LOW);
            }
		}
	}
    else if(res_flash!=FR_OK)
    {
        printf("extern Flash mount filesystem failed.(%d)\r\n",res_flash);
        printf("maybe due to:SPI Flash init failed.\r\n");
		while(1)
        {
            delay_ms(200);
            pin_put(LED0,PIN_HIGH);
            delay_ms(200);
            pin_put(LED0,PIN_LOW);
        }
    }
    else
    {
        printf(">>>filesystem mount success,you can do some read and write test\r\n");
    } 
    

/*----------------------- 文件系统测试：写测试 -------------------*/
    /* 打开文件，每次都以新建的形式打开，属性为可写 */
	printf("\r\n****** ready to read test... ******\r\n");	
	res_flash = f_open(&fnew, "1:read_write.txt",FA_CREATE_ALWAYS | FA_WRITE );
	if ( res_flash == FR_OK )
	{
		printf(">>>open/create read_write.txt success.now going to write test.\r\n");
        /* 将指定存储区内容写入到文件内 */
		res_flash=f_write(&fnew,WriteBuffer,sizeof(WriteBuffer),&fnum);
        if(res_flash==FR_OK)
        {
            printf(">>>file write success,write into byte data:%d\n",fnum);
            printf(">>>data you write to file is:\r\n%s\r\n",WriteBuffer);
        }
        else
        {
          printf(">>>file write failed(%d)\n",res_flash);
        }    
		/* 不再读写，关闭文件 */
        f_close(&fnew);
	}
	else
	{	
		printf(">>>open/create file failed!\r\n");
	}
	
    
/*------------------- 文件系统测试：读测试 --------------------------*/
	printf("****** ready to read test... ******\r\n");
	res_flash = f_open(&fnew, "1:read_write.txt",FA_OPEN_EXISTING | FA_READ); 	 
	if(res_flash == FR_OK)
	{
		printf(">>>file open success!\r\n");
		res_flash = f_read(&fnew, ReadBuffer, sizeof(ReadBuffer), &fnum); 
        if(res_flash==FR_OK)
        {
            printf(">>>file read success,size you read is:%d\r\n",fnum);
            printf(">>>file data you read is:\r\n%s \r\n", ReadBuffer);	
        }
        else
        {
          printf(">>>file read failed(%d)\n",res_flash);
        }		
	}
	else
	{
		printf(">>>file open failed!\r\n");
	}
	/* 不再读写，关闭文件 */
	f_close(&fnew);	
    
    /*读取main.py*/
    printf("now is going to read main.py");
    res_flash = f_open(&fnew, "1:main.py",FA_OPEN_EXISTING | FA_READ);
    if(res_flash == FR_OK)
    {
        res_flash = f_read(&fnew, ReadBuffer, sizeof(ReadBuffer), &fnum); 
        if(res_flash==FR_OK)
        {
            printf(">>>file read success,size you read is:%d\r\n",fnum);
            printf("\r\n====================main.py=====================[X]\r\n");
            printf(">>>file data you read is:\r\n%s \r\n", ReadBuffer);	
        }
    }
    f_close(&fnew);	
    
	/* 不再使用文件系统，取消挂载文件系统 */
	f_mount(NULL,"1:",1);    

#else
    
/*-----------------------初始化USB-------------------------------*/   

	/*初始化(已经初始化FLASH就不用再次初始化了)*/
	Set_System();
  	
	/*设置USB时钟为48M*/
	Set_USBClock();
 	
	/*配置USB中断(包括SDIO中断)*/
	USB_Interrupts_Config();
 
	/*USB初始化*/
 	USB_Init();
 
     /*等待配置完成*/  
 	while (bDeviceState != CONFIGURED)
    {
    }
    
#endif    
    
    while(1)
    {
        delay_ms(500);
        pin_put(LED0,PIN_HIGH);
        delay_ms(500);
        pin_put(LED0,PIN_LOW);
    }
}








